package cn.android666.client

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.IBinder
import android.util.Log
import android.widget.Button
import cn.android666.remoteservice.IDzEngine

class MainActivity : AppCompatActivity(), ServiceConnection {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        bindRemoteService()
    }

    private fun bindRemoteService() {
        val intent = Intent("cn.android666.remoteservice.hahaha") // action为远程服务的action
        intent.setPackage("cn.android666.remoteservice") // 包名为远程服务的的包名
        val result = bindService(intent, this, Context.BIND_AUTO_CREATE)
        Log.i("ABCD", "绑定函数返回结果：$result")
    }

    /** 在与 Service 的连接建立时调用 */
    override fun onServiceConnected(name: ComponentName, service: IBinder) {
        val dzEngine = IDzEngine.Stub.asInterface(service)
        val hello = dzEngine.hello("张三")
        Log.i("ABCD","远程服务绑定成功: $hello")
    }

    /** 当与服务的连接被意外中断时执行，这通常发生在服务的进程崩溃或被终止时。 */
    override fun onServiceDisconnected(name: ComponentName?) {
        Log.i("ABCD","远程服务的连接被意外中断")
    }

}